package com.example.todolist.controller;

import com.example.todolist.model.Todo;
import com.example.todolist.repository.TodoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Optional;

// 标记为 Controller，处理 Web 请求
@Controller
@RequestMapping("/todos") // 所有方法都以 /todos 开头
public class TodoController {

    private final TodoRepository todoRepository;

    // 构造函数注入 Repository (Spring 推荐方式)
    @Autowired
    public TodoController(TodoRepository todoRepository) {
        this.todoRepository = todoRepository;
    }

    // ----------------------------------------------------
    // 1. GET /todos: 显示所有任务列表
    // ----------------------------------------------------
    @GetMapping
    public String listTodos(Model model) {
        // 从数据库获取所有任务，并添加到 Model 中，供 Thymeleaf 访问
        model.addAttribute("todos", todoRepository.findAll());
        // 返回模板文件路径：classpath:/templates/todos/list.html
        return "todos/list";
    }

    // ----------------------------------------------------
    // 2. GET /todos/new: 显示创建任务的表单
    // ----------------------------------------------------
    @GetMapping("/new")
    public String showNewTodoForm(Model model) {
        // 创建一个空的 Todo 对象，用于表单绑定
        model.addAttribute("todo", new Todo());
        model.addAttribute("isNew", true); // 标记为创建模式
        // 返回模板文件路径：classpath:/templates/todos/from.html
        return "todos/from";
    }
    @PostMapping
    public String saveTodo(@ModelAttribute Todo todo){
        todoRepository.save(todo);
        return "redirect:/todos";
    }
    @GetMapping("/edit/{id}")
    public String showEditTodoForm(@PathVariable Long id, Model model){
        Optional<Todo> todo = todoRepository.findById(id);
        if (todo.isPresent()){
            model.addAttribute("todo", todo.get());
            model.addAttribute("isNew", false);
            return "todos/form"; // 修复：将 "todos/from" 改为 "todos/form"
        }
        return "redirect:/todos";
    }


    @PostMapping("/{id}")
    public String updateTodo(@PathVariable Long id,@ModelAttribute Todo todo){
        Optional<Todo> existingTodoOpt=todoRepository.findById(id);
        if (existingTodoOpt.isPresent()){
            Todo existingTodo=existingTodoOpt.get();

            existingTodo.setDescription(todo.getDescription());
            existingTodo.setCompleted(todo.isCompleted());

            todoRepository.save(existingTodo);
        }
        return "redirect:/todos";
    }

    @GetMapping("/delete/{id}")
    public String deleteTodo(@PathVariable Long id){
        todoRepository.deleteById(id);
        return "redirect:/todos";
    }
    @GetMapping("/toggle/{id}")
    public String toggleTodoCompletion(@PathVariable Long id){
        Optional<Todo> todoOpt=todoRepository.findById(id);

        if (todoOpt.isPresent()){
            Todo todo=todoOpt.get();
            todo.setCompleted(!todo.isCompleted());
            todoRepository.save(todo);
        }
        return "redirect:/todos";
    }
}